/**
 * 初始化机构详情对话框
 */
var OfficeInfoDlg = {
    officeInfoData : {},
    zTreeInstance : null,
    zTreeInstance2 : null
};

/**
 * 清除数据
 */
OfficeInfoDlg.clearData = function() {
    this.officeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
OfficeInfoDlg.set = function(key, val) {
    this.officeInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
OfficeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

OfficeInfoDlg.select = function (key) {
	return $("#"+key).find("option:selected").val();
};
/**
 * 关闭此对话框
 */
OfficeInfoDlg.close = function() {
    parent.layer.close(window.parent.Office.layerIndex);
}

/**
 * 点击机构ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
OfficeInfoDlg.onClickOffice = function(e, treeId, treeNode) {
    $("#parentName").attr("value", OfficeInfoDlg.zTreeInstance.getSelectedVal());
    $("#parentId").attr("value", treeNode.id);
    //选择机构后，隐藏树
    OfficeInfoDlg.hideOfficeSelectTree();
}

OfficeInfoDlg.onClickCompany = function(e, treeId, treeNode) {
    $("#companyName").attr("value", OfficeInfoDlg.zTreeInstance2.getSelectedVal());
    $("#fback").attr("value", treeNode.id);
    //选择机构后，隐藏树
    OfficeInfoDlg.hideCompanySelectTree();
}


OfficeInfoDlg.showOutCompany = function(){
	var type = this.select("type");
	console.log(type);
	 $("#companyName").attr("value", "");
	 $("#fback").attr("value", "");
	 $("#parentName").attr("value", "");
	 $("#parentId").attr("value", "");
	
	if("1" == type){
		//$("#outCompany").hide();
		OfficeInfoDlg.initOffice("1");
	}else{
		//$("#outCompany").show();
		OfficeInfoDlg.initOffice("0");
		//OfficeInfoDlg.initCompany("1");
	}
}

OfficeInfoDlg.initOffice = function(type){
	if(OfficeInfoDlg.zTreeInstance){
		OfficeInfoDlg.zTreeInstance= null;
	}
	var ztree = new $ZTree("parentOfficeMenuTree", "/sys/sysOffice/tree/"+type);
	ztree.bindOnClick(OfficeInfoDlg.onClickOffice);
	ztree.init();
	OfficeInfoDlg.zTreeInstance = ztree;
}

OfficeInfoDlg.initCompany = function(type){
	if(OfficeInfoDlg.zTreeInstance2){
		OfficeInfoDlg.zTreeInstance2 = null;
	}
	var ztree = new $ZTree("parentCompanyMenuTree", "/sys/sysOffice/tree/"+type);
	ztree.bindOnClick(OfficeInfoDlg.onClickCompany);
	ztree.init();
	OfficeInfoDlg.zTreeInstance2 = ztree;
}



/**
 * 显示机构选择的树
 *
 * @returns
 */
OfficeInfoDlg.showOfficeSelectTree = function() {
    var pName = $("#parentName");
    var pNameOffset = $("#parentName").offset();
    $("#parentOfficeMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}
OfficeInfoDlg.showCompanySelectTree = function() {
    var pName = $("#companyName");
    var pNameOffset = $("#companyName").offset();
    $("#parentCompanyMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏机构选择的树
 */
OfficeInfoDlg.hideOfficeSelectTree = function() {
    $("#parentOfficeMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}
OfficeInfoDlg.hideCompanySelectTree = function() {
    $("#parentCompanyMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
OfficeInfoDlg.collectData = function() {
    this.set('id').set('name').set('simplename').set('sort')
    .set('parentId').set('type',$('#type').find("option:selected").val())
    .set('grade').set('address').set('zipCode').set('master').set('phone')
    .set('fax').set('email').set('remarks').set('compCode').set("enname")
    .set('fback');
}

/**
 * 验证数据非空
 */
OfficeInfoDlg.provingNull = function () {
	
    if(this.get('name') && this.get('parentName') 
    		){
    	return true;
    } else {
    	return false;
    }
};

/**
 * 验证邮箱及手机号数据
 */
OfficeInfoDlg.provingData = function () {
	var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
    if (reg.test(this.get('email')) && (/^1[3|4|5|7|8]\d{9}$/.test(this.get('phone')))) {
    	return true;
    } else {
    	return false;
    }
};

/**
 * 提交添加机构
 */
OfficeInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    
    if (!this.provingNull()) {
        Feng.error("请确认必填项");
        return;
    }
    if (!this.provingData()) {
        Feng.error("邮箱格式或手机号不正确,请重新填写");
        return;
    }
    
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/sys/sysOffice/save/add", function(data){
    	if(data.resultCode == 1){
    		Feng.success("添加成功!");
            window.parent.Office.table.refresh();
            OfficeInfoDlg.close();
            OfficeTableToolbar.close();
    	}else{
    		 Feng.error("添加失败!" + data.msg + "!");
    	}
        
    },function(data){
       
    });
    ajax.set(this.officeInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
OfficeInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.provingNull()) {
        Feng.error("请确认必填项");
        return;
    }
    if (!this.provingData()) {
        Feng.error("邮箱格式或手机号不正确,请重新填写");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/sys/sysOffice/save/update", function(data){
    	if(data.resultCode == 1){
    		Feng.success("修改成功!");
            window.parent.Office.table.refresh();
            OfficeInfoDlg.close();
    	}else{
    		Feng.error("修改失败!" + data.msg + "!");
    	}
    	
    },function(data){
    	Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
                                          
    ajax.set(this.officeInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentOfficeMenu" || 
    		$(event.target).parents("#parentOfficeMenu").length > 0 )) {
        OfficeInfoDlg.hideOfficeSelectTree();
    }
    if (!(event.target.id == "menuBtn" || event.target.id == "parentCompanyMenu" || 
    		$(event.target).parents("#parentCompanyMenu").length > 0 )) {
        OfficeInfoDlg.hideCompanySelectTree();
    }
}

$(function() {
	var type = $("#typeVal").val();
    $("#type").val(type);
    console.log(type);
    if("1" == type){
		//$("#outCompany").hide();
		OfficeInfoDlg.initOffice("1");
	}else{
		//$("#outCompany").show();
		OfficeInfoDlg.initOffice("0");
		//OfficeInfoDlg.initCompany("1");
	}
});
